package com.business_clean.ui.activity.personnel;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.PhoneUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.ext.CustomViewExtKt;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.uitl.DividerItemDecoration;
import com.business_clean.data.mode.project.ProjectManager;
import com.business_clean.data.mode.roster.RosterEntity;
import com.business_clean.data.mode.todo.TodoList;
import com.business_clean.databinding.ActivitySelectPersonnelBinding;
import com.business_clean.ui.activity.depart.DepartActivity;
import com.business_clean.ui.activity.depart.QuickDepartActivity;
import com.business_clean.ui.adapter.personnel.SelectPersonnelAdapter;
import com.business_clean.ui.adapter.roster.RosterRowAdapter;
import com.business_clean.viewmodel.request.DepartViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import me.hgj.mvvmhelper.base.BaseViewModel;
import me.hgj.mvvmhelper.net.LoadStatusEntity;

public class SelectPersonnelActivity extends BaseActivity<DepartViewModel, ActivitySelectPersonnelBinding> {
    private int page = 1;
    private int requestType;
    private SelectPersonnelAdapter mAdapter;

    private int isMultiple = 0;

    private int role_id = 0;//1管理员 2项目负责人 3人事 4领班 5保洁 6大区经理

    private int channel = 0;//如果是 0 默认就是人员列表 如果是 1 那么就是休息人员

    private int is_head_office;//是否总部员工0全部 1是 2否
    private RosterRowAdapter rowAdapter;//旁边导航

    private String project_uuid;
    private String class_uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            isMultiple = getIntent().getExtras().getInt("multiple");//0是不选择，默认跳转离职档案，1 是单选，2是多选
            role_id = getIntent().getExtras().getInt("role_id");//比如说你要筛选那个类型的，你就指定角色 id
            channel = getIntent().getExtras().getInt("channel");//用来区分 获取休息人员，还是人员列表  默认是 0
            is_head_office = getIntent().getExtras().getInt("is_head_office");//是否筛选总部人员
            mDatabind.recyclerRow.setVisibility(getIntent().getExtras().getBoolean("row_visible", false) ? View.VISIBLE : View.GONE);


            switch (role_id) {
                case 1:
                    mDatabind.tabbar.setTitle("选择管理员");
                    break;
                case 2:
                    mDatabind.tabbar.setTitle("选择项目负责人");
                    break;
                case 3:
                    mDatabind.tabbar.setTitle("选择人事");
                    break;
                case 4:
                    mDatabind.tabbar.setTitle("选择领班");
                    break;
                case 5:
                    mDatabind.tabbar.setTitle("选择保洁员");
                    break;
                case 6:
                    mDatabind.tabbar.setTitle("选择大区经理");
                    break;
                default:
                    String title = getIntent().getExtras().getString("tab_title");
                    if (!TextUtils.isEmpty(title)) {
                        mDatabind.tabbar.setTitle(title);
                    }
                    break;
            }
        }

        if (isMultiple != 2) {
            mDatabind.bottom.llBaseButtomButton.setVisibility(View.GONE);
        } else {
            mDatabind.bottom.llBaseButtomButton.setVisibility(View.VISIBLE);
        }

        if (1 == channel) {
            project_uuid = App.appViewModelInstance.getProjectInfo().getValue().getUuid();
            class_uuid = getIntent().getExtras().getString("class_uuid");
        }

        mAdapter = new SelectPersonnelAdapter(isMultiple);
        CustomViewExtKt.initRecyclerViewVertical(mDatabind.list.recyclerView, mAdapter);
        mDatabind.list.recyclerView.addItemDecoration(new DividerItemDecoration(this));

        //row
        rowAdapter = new RosterRowAdapter();
        getMDatabind().recyclerRow.setLayoutManager(new LinearLayoutManager(this));
        getMDatabind().recyclerRow.setAdapter(rowAdapter);


        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });

        //如果需要左边侧栏，那么请求这个接口，不需要直接请求列表
        if (mDatabind.recyclerRow.getVisibility() == View.VISIBLE) {
            mViewModel.getProjectMangerList("1");
        } else {
            requestOne();
        }
    }

    @Override
    public void onBindViewClick() {
        //点击
        rowAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                project_uuid = rowAdapter.getData().get(position).getUuid();
                rowAdapter.updateItem(position);
                requestOne();
            }
        });
        //确定选择的人员
        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMultiple != 0) {//只有多选的时候才处理
                    App.appViewModelInstance.getSelectedPersonnel().setValue(mAdapter.getSelectData());
                    finish();
                }
            }
        });
        //列表点击
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (isMultiple == 2) {
                    mAdapter.updateItem(position);
                    return;
                } else if (isMultiple == 1) {
                    mAdapter.updateItem(position);
                    App.appViewModelInstance.getSelectedPersonnel().setValue(mAdapter.getSelectData());
                    finish();
                    return;
                }

                Bundle bundle = new Bundle();
                bundle.putString("uuid", mAdapter.getData().get(position).getUuid());
                ActivityForwardUtil.startActivity(QuickDepartActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.iv_item_roster_call) {
                    PhoneUtils.dial(mAdapter.getData().get(position).getMobile());
                }
            }
        });
//        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ActivityForwardUtil.startActivity(DepartActivity.class);
//            }
//        });
    }


    @Override
    public void onRequestSuccess() {
        //旁边导航
        mViewModel.getAllProjectManager().observe(this, new Observer<ProjectManager>() {
            @Override
            public void onChanged(ProjectManager projectManager) {
                rowAdapter.setList(projectManager.getList());
                if (rowAdapter.getData().size() > 0) {
                    project_uuid = rowAdapter.getData().get(0).getUuid();
                    requestOne();
                }
            }
        });

        //人员列表
        mViewModel.getStaffManager().observe(this, new Observer<RosterEntity>() {
            @Override
            public void onChanged(RosterEntity data) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(data.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(data.getList());
                        }
                        break;
                }

                if (isMultiple == 2) {
                    String uuids = getIntent().getExtras().getString("uuids");
                    //如果是多选的话，地址反选一下
                    mAdapter.dataInvert(uuids);
                }
            }
        });
    }

    @Override
    public void onRequestError(@NonNull LoadStatusEntity loadStatus) {
        mDatabind.list.refreshLayout.finishRefresh();
        mDatabind.list.refreshLayout.finishLoadMore();
    }

    void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        if (0 == channel) {
            mViewModel.getStaffList(page, 1, role_id, is_head_office, project_uuid);
        } else if (1 == channel) {
            mViewModel.requestRestList(project_uuid, class_uuid);
        }
    }

    void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        if (0 == channel) {
            mViewModel.getStaffList(page, 1, role_id, is_head_office, project_uuid);
        } else if (1 == channel) {
            mViewModel.requestRestList(project_uuid, class_uuid);
        }
    }
}